#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/7/20 10:28
# @Author  : Sun
# @File    : base_api.py
# @Software: PyCharm
import logging

import pytest
import requests
from common.write_log import WriteLog

from log.log_set import LogSet

"""
获取token，作为其他API类的父类
"""
class BaseApi:
    # 定义连接MQTT的UID
    _JJUID = '682139016'
    _URL = "https://test-api.live.jjmatch.com:21102/account/JJLogin"
    _HEADERS = {
        'Host': 'test-api.live.jjmatch.com:21102',
        'User-Agent': 'okhttp/3.12.0',
        'platform': 'PC-Client',
        'Content-Type': 'application/x-www-form-urlencoded'
    }
    _DATA = f'JJUid={_JJUID}&JJToken=zb123456&loginType=4'

    def __init__(self):
        self.result = None
        self.token = None

    """
    登录并获取token
    """
    def login(self):
        r = requests.request(method='POST', url=self._URL, headers=self._HEADERS, data=self._DATA)
        self.result = r.json()
        # WriteLog().write_log().info(f'登录API请求结果{self.result}')
        self.token = self.result.get('data').get('token')
        WriteLog().write_log().info(f'登录API请求结果 - Token{self.token}')
        return self.token


if __name__ == '__main__':
    BaseApi().login()
